package com.zzptc.zzsgz.bdsjws.activities;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.widget.RelativeLayout;
import com.zzptc.zzsgz.bdsjws.Adapter.BBXAdapter;
import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.base.BaseActivity;
import com.zzptc.zzsgz.bdsjws.db.BBXDao;
import com.zzptc.zzsgz.bdsjws.utils.DividerGridItemDecoration;
import com.zzptc.zzsgz.bdsjws.vo.BBX;
import java.util.Collections;
import java.util.List;
import butterknife.BindView;


public class BBXActivity extends BaseActivity {
    List<BBX> list;
    BBXAdapter bbxAdapter;
    @BindView(R.id.activity_bbx)
    RelativeLayout activityBbx;
    @BindView(R.id.rv_bbx)
    RecyclerView rvBbx;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bbxAdapter = new BBXAdapter(list);


        BBXDao bbxDao = MyApplication.getDaoSession().getBBXDao();
        list = bbxDao.loadAll();
        System.out.println(list.size() + "}}}}}}}}}}}}}}}}}]]");

        //用RecyclerView显示数据 显示4*4
        bbxAdapter = new BBXAdapter(list);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this , 4);
        rvBbx.setLayoutManager(layoutManager);
        rvBbx.addItemDecoration(new DividerGridItemDecoration(this));
        rvBbx.setAdapter(bbxAdapter);
//        rvBbx.addOnItemTouchListener(new OnRecyclerItemClickListener(rvBbx){
//            @Override
//            public void onLongClick(RecyclerView.ViewHolder vh) {
//                super.onLongClick(vh);
//            }
//
//            @Override
//            public void onItemClick(RecyclerView.ViewHolder vh) {
//                super.onItemClick(vh);
//                BBX bbx = list.get(vh.getPosition());
//                if (bbx.getName().equals("家人守护")){
//                    startActivity(new Intent(BBXActivity.this , FamilyProtectActivity.class));
//                }
//            }
//        });











        ItemTouchHelper helper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                //首先回调的方法 返回int表示是否监听该方向
//                int dragFlags = ItemTouchHelper.UP|ItemTouchHelper.DOWN;//拖拽
                int swipeFlags = ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT;//侧滑删除
                int dragFlags = ItemTouchHelper.UP|ItemTouchHelper.DOWN|ItemTouchHelper.LEFT| ItemTouchHelper.RIGHT;//拖拽  
//                int swipeFlags = 0;//侧滑删除
                return makeMovementFlags(dragFlags,swipeFlags);
            }

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                //滑动事件
                Collections.swap(list,viewHolder.getAdapterPosition(),target.getAdapterPosition());
                bbxAdapter.notifyItemMoved(viewHolder.getAdapterPosition(),target.getAdapterPosition());
                return false;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                //侧滑事件
                list.remove(viewHolder.getAdapterPosition());
                bbxAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
            }

            @Override
            public boolean isLongPressDragEnabled() {
                //是否可拖拽
                return true;
            }
        });
        helper.attachToRecyclerView(rvBbx);


    }

    @Override
    public int getContentXML() {
        return R.layout.activity_bbx;
    }

    @Override
    public void initData() {
//        BBXDao bbxDao = MyApplication.getDaoSession().getBBXDao();
//        list = bbxDao.loadAll();
//        System.out.println(list.size() + "}}}}}}}}}}}}}}}}}]]");
//
//        //用RecyclerView显示数据 显示4*4
//        BBXAdapter bbxAdapter = new BBXAdapter(list);
//        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this , 4);
//        rvBbx.setLayoutManager(layoutManager);
//        rvBbx.addItemDecoration(new DividerGridItemDecoration(this));
//        rvBbx.setAdapter(bbxAdapter);

    }

    @Override
    public void initView() {

    }
}
